<?php

/**
 * User: Aven(老甘)
 * Date: 2018-03-20
 * Time: 16:15
 */
class Worker1 extends \think\worker\Server
{
    protected $socket = 'websocket://gitee88.com:2346';

    private $_name =[];

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        if (!$this->is_json($data)){
            $connection->send('非法请求');
            return;
        }

        $data = json_decode($data,true);

        if (!isset($data['type'])){
            $connection->send('非法请求');
            return;
        }

        switch ($data['type']){
            case 'message':
                foreach ( $this->worker->connections as $_connection ){
                    $_connection->send($this->createData('message',['name'=>$this->getName($connection->id),'message'=>$data['data']['message']]));
                }

                break;
            case 'setName':
                $this->setName($connection->id, $data['data']['name']);
                $connection->send($this->createData('msg',['msg'=>'设置昵称为:'.$data['data']['name']]));
                break;
            case 'push':
                if ($data['id']){
                    if (isset($this->worker->connections[$data['id']])){
                        $this->worker->connections[$data['id']]->send($this->createData('message',['name'=>'后台','message'=>$data['data']]));
                    }else{
                        $connection->send($this->createData('result',['msg'=>'连接不存在'],1));
                    }

                }else{
                    foreach ( $this->worker->connections as $_connection ){
                        if ($connection->id != $_connection->id)
                            $_connection->send($this->createData('message',['name'=>'后台','message'=>$data['data']]));
                    }
                }

                $connection->send($this->createData('result',['msg'=>'推送成功']));
                break;
        }

    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        //设置默认昵称
        $this->setName($connection->id,'匿名'.rand(1000,9999));
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        unset($this->_name[$connection->id]);
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {

    }

    private function createData($type,$data,$code=0){
        $_data = [
            'code'  => $code,
            'type'  => $type,
            'data'  => $data
        ];
        return json_encode($_data);
    }

    private function setName($id,$name){
        $this->_name[$id] = $name;
    }

    private function getName($id){
        return $this->_name[$id];
    }

    private function is_json($str)
    {
        json_decode($str);
        return(json_last_error() == JSON_ERROR_NONE) && !is_numeric($str)  ;
    }

}